package br.com.localfloripa.util;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
	
	private static String[] REPLACES = { "a", "e", "i", "o", "u", "c", "A", "E", "I", "O", "U", "C" };  	  
	private static Pattern[] PATTERNS = null;  

	public StringUtils() {
		
	}
	
	public static String randomName() {
		
		Random rand = new Random();
		return Long.toHexString(rand.nextLong());
		
	}
	
	private static void compilePatterns() {  
		PATTERNS = new Pattern[REPLACES.length];  
		PATTERNS[0]  = Pattern.compile("[âãáàä]");  
		PATTERNS[1]  = Pattern.compile("[éèêë]");  
		PATTERNS[2]  = Pattern.compile("[íìîï]");  
		PATTERNS[3]  = Pattern.compile("[óòôõö]");  
		PATTERNS[4]  = Pattern.compile("[úùûü]");  
		PATTERNS[5]  = Pattern.compile("[ç]");  
		PATTERNS[6]  = Pattern.compile("[ÂÃÁÀÄ]");  
		PATTERNS[7]  = Pattern.compile("[ÉÈÊË]");  
		PATTERNS[8]  = Pattern.compile("[ÍÌÎÏ]");  
		PATTERNS[9]  = Pattern.compile("[ÓÒÔÕÖ]");  
		PATTERNS[10] = Pattern.compile("[ÚÙÛÜ]");  
		PATTERNS[11] = Pattern.compile("[Ç]");  
	}  
	  
   /** 
	* Substitui os caracteres acentuados por nao acentuados. 
	*  
	* @param text 
	* @return 
	*/  
	public static String replaceSpecial(String text) {  
		if (PATTERNS == null) {  
			compilePatterns();  
		}  
  
		String result = text;  
		for (int i = 0; i < PATTERNS.length; i++) {  
			Matcher matcher = PATTERNS[i].matcher(result);  
			result = matcher.replaceAll(REPLACES[i]);  
		}  
		return result;  
	}  
	
	public static String arrayToString(String[] a, String separator) {
	    String result = "";
	    if (a.length > 0) {
	        result = a[0];    // start with the first element
	        for (int i=1; i<a.length; i++) {
	            result = result + separator + a[i];
	        }
	    }
	    return result;
	}
	
	public static int occurrenceString(String frase,String sub) {
		
		int count = 0;
		for(int i = 0; i < (frase.length() - sub.length() + 1); i++){
	        String res = frase.substring(i, (i + sub.length()));
	        if(res.equals(sub)){
	            count++;
	        }
	    }
		return count;
	}
	 
}
